<?php
/**
 * @author: huhaiqin
 * @Date: 2023-04-03
 * @desc: 自定义页面
 */
namespace Game\Model;
use Think\Model;

class PageModuleModel extends Model{

    private $cache_name = 'PAGE_MODULE'; //获取页面模块

    /**
     * huhaiqin，2023.04.03
     * 获取全部
     */
    public function _get_all(){
        $data = F($this->cache_name); //读取缓存
        // 暂时去掉缓存
        if(!$data || true){
            $all_data = $this->where('status = 1')->order('listorder desc')->select(); //仅输出状态为显示的模块
            $data = [];
            foreach($all_data as $t){
                $t['data'] = json_decode($t['data'],true);
                $data[] = $t;
            }
            F($this->cache_name,$data);
        }
        return $data; //返回数据
    }

    /**
     * 添加、编辑
     */
    public function _edit($post = []){
        // $post['data'] = html_entity_decode($post['data']);
        // $post['data'] = json_decode($post['data']);
        $post['data'] = json_encode($post['data'],JSON_UNESCAPED_UNICODE);
        $post['update_time'] = time();
        // $data = $this->create($post);
        $data = $post;
        F($this->cache_name,NULL);
        if($data['id']){
            //编辑
            $rdata = $this->save($data);
            if($rdata){
                _apiReturn(1,'success', $rdata);
            }else{
                _apiReturn(0,'error');
            }
        }else{
            //添加
            $rdata = $this->add($data);
            if($rdata){
                _apiReturn(1,'success', $rdata);
            }else{
                _apiReturn(0,'error');
            }
        }
    }

    /**
     * 物理删除
     */
    public function _del($ids = []){
        if(count($ids) == 0){
            _apiReturn(0,'ID不能为空~');
        }
        $map['id'] = ['in',$ids];
        if($this->where($map)->delete()){
            F($this->cache_name,NULL); //清楚缓存
            _apiReturn(1,'success');
        }else{
            _apiReturn(0,'error');
        }
    }

    /**
     * 数据接口
     */
    public function _query($p = 1, $row = 10, $query = ''){
        $sql = 'SELECT A.* FROM mini_page_module A WHERE 1=1 ';
        $sqlcount = 'SELECT COUNT(A.`id`) AS tp_count FROM mini_page_module A WHERE 1=1 ';
        if ($query) {
            $sql .= $query;
            $sqlcount .= $query;
        }
        $sql .= ' ORDER BY A.listorder desc ';
        $sql .= ' LIMIT '.($p - 1) * $row.','.$row;
        $total = $this->query($sqlcount);
        $data['data'] = $this->query($sql);
        $data['page']['total'] = $total[0]['tp_count'];
        $data['page']['cur'] = $p;
        $data['page']['row'] = $row;
        // $data['sql'] = $sql;
        return $data;
    }
    
}